package ui;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import pd.*;

import javax.swing.*;
import javax.swing.table.*;

public class AddrBook extends JFrame implements Observer {
	
	private static final long serialVersionUID = 1L;
	private SwissSMSClient swissSMSClient;
	private JTable contacts = new JTable();
	private JButton newcontact = new JButton("New");
	private JButton delcontact = new JButton("Delete");
	private NewContact newContact;
	
	public AddrBook(SwissSMSClient swissSMSClient){
		super("AddressBook");
		this.swissSMSClient = swissSMSClient;
		swissSMSClient.getStorage().addObserver(this);
		getContentPane().setLayout(new BorderLayout());
		initActions();
		initContacts();
		initUI();
		setResizable(false);
		pack();
		setLocationRelativeTo(null);
	}
	
	private void initActions(){
		newcontact.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(newContact == null){
					newContact = new NewContact();
				}
				newContact.setVisible(true);
			}
		});
		
		delcontact.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				int pos = contacts.getSelectedRow();
				if(pos != -1){
					swissSMSClient.getSetting("contacts").remove(pos);
					swissSMSClient.getStorage().saveSettings();
				}
			}
		});
	}
	
	private void initContacts(){
		contacts.setModel(new AbstractTableModel() {
			private static final long serialVersionUID = 1L;
			private Vector<String> contactsv = swissSMSClient.getSetting("contacts");
			public int getColumnCount() {return 2;}
			public int getRowCount() {return contactsv.size();}
			public Object getValueAt(int row, int col) {
				String retval = contactsv.get(row);
				switch(col){
					case 0:
						retval = retval.substring(0, retval.indexOf('\\'));
						break;
					case 1:
						retval = retval.substring(retval.indexOf('\\')+1);
						break;
					default:
						retval = "";
						break;
				}
				return retval;
			}
		});
		contacts.getColumn("A").setHeaderValue("Name");
		contacts.getColumn("B").setHeaderValue("Number");
	}
	
	private void initUI(){
		Box inputbox = Box.createVerticalBox();
		inputbox.add(Box.createVerticalStrut(20));
		
		getContentPane().add(inputbox, BorderLayout.NORTH);
		JScrollPane historyScroll = new JScrollPane(contacts);
		getContentPane().add(historyScroll, BorderLayout.CENTER);
		
		Box buttonsbox = Box.createHorizontalBox();
		buttonsbox.add(Box.createHorizontalGlue());
		buttonsbox.add(newcontact);
		buttonsbox.add(delcontact);
		buttonsbox.add(Box.createHorizontalStrut(20));
		inputbox.add(buttonsbox);
		inputbox.add(Box.createVerticalStrut(10));
		
		getContentPane().add(inputbox, BorderLayout.SOUTH);
	}

	public void update(Observable o, Object arg) {
		initContacts();
	}
	
	
	private class NewContact extends JDialog{
		private static final long serialVersionUID = 1L;
		
		private JTextField name = new JTextField();
		private JTextField number = new JTextField();
		private JButton cancel = new JButton("Cancel");
		private JButton save = new JButton("Save");
		
		public NewContact(){
			setUndecorated(true);
			setModal(true);
			setResizable(false);
			getRootPane().setDefaultButton(save);
			initActions();
			initUI();
			pack();
			setLocationRelativeTo(null);
		}
		
		public void setVisible(boolean b){
			super.setVisible(b);
			name.setText("");
			number.setText("");
		}
		
		private void initActions(){
			save.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					if(!name.getText().matches("") && !number.getText().matches("")){
						swissSMSClient.getSetting("contacts").add(name.getText() + "\\" + number.getText());
						AddrBook.this.swissSMSClient.getStorage().saveSettings();
					}
					setVisible(false);
				}
			});
			cancel.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					setVisible(false);
				}
			});
		}
		
		private void initUI(){
			Box inputbox = Box.createVerticalBox();
			inputbox.add(Box.createVerticalStrut(20));
			
			Box namebox = Box.createHorizontalBox();
			namebox.add(Box.createHorizontalStrut(20));
			namebox.add(new JLabel("Name: "));
			setSize(name, 100, 30);
			namebox.add(name);
			namebox.add(Box.createHorizontalGlue());
			inputbox.add(namebox);
			inputbox.add(Box.createVerticalStrut(10));
			
			Box numberbox = Box.createHorizontalBox();
			numberbox.add(Box.createHorizontalStrut(20));
			numberbox.add(new JLabel("Number: "));
			setSize(number, 100, 30);
			numberbox.add(number);
			numberbox.add(Box.createHorizontalGlue());
			inputbox.add(numberbox);
			inputbox.add(Box.createVerticalStrut(10));
			
			Box buttonsbox = Box.createHorizontalBox();
			buttonsbox.add(Box.createHorizontalGlue());
			buttonsbox.add(cancel);
			buttonsbox.add(save);
			buttonsbox.add(Box.createHorizontalStrut(20));
			inputbox.add(buttonsbox);
			inputbox.add(Box.createVerticalStrut(10));
			
			getContentPane().add(inputbox, BorderLayout.SOUTH);
		}
		
		private void setSize(Component c, int dx, int dy){
			c.setPreferredSize(new Dimension(dx,dy));
			c.setMinimumSize(new Dimension(dx,dy));
			c.setMaximumSize(new Dimension(dx,dy));
		}
	}

}
